package xin.marcher.module.diplomacy.api.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.common.constants.diplomacy.HistorySearchWordConstants;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.redis.RedisCache;
import xin.marcher.module.diplomacy.api.ProductSearchApi;
import xin.marcher.module.diplomacy.domain.dto.CompletionSearchWordsDTO;
import xin.marcher.module.diplomacy.domain.dto.HistorySearchWordDTO;
import xin.marcher.module.diplomacy.domain.dto.HistorySearchWordResultDTO;
import xin.marcher.module.diplomacy.domain.dto.PorductSearchDTO;
import xin.marcher.module.diplomacy.domain.query.*;
import xin.marcher.module.diplomacy.repository.ProductSearchRepository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品搜索
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = ProductSearchApi.class, retries = 0)
public class ProductSearchApiImpl implements ProductSearchApi {

    @Resource
    private RedisCache redisCache;

    @Resource
    private ProductSearchRepository productSearchRepository;

    /**
     * 保存历史搜索词接口
     *
     * @param request
     */
    @Override
    public JsonResult<HistorySearchWordResultDTO> saveHistorySearchWord(HistorySearchWordRequest request) {
        // 在队列头部添加新的历史搜索词
        redisCache.lpush(HistorySearchWordConstants.getKey(request.getUserId()), request.getHistorySearchWord());
        // 修建队列只保存固定数量的搜索词
        redisCache.ltrim(HistorySearchWordConstants.getKey(request.getUserId()), 0,
                HistorySearchWordConstants.HISTORY_WORD_COUNT_PER_USER - 1);
        return JsonResult.buildSuccess(new HistorySearchWordResultDTO(true));
    }

    /**
     * 查询历史搜索词接口
     *
     * @param request
     */
    @Override
    public JsonResult<HistorySearchWordDTO> listHistorySearchWords(HistorySearchWordQuery request) {
        List<String> result = redisCache.lrange(HistorySearchWordConstants.getKey(request.getUserId()), 0,
                HistorySearchWordConstants.HISTORY_WORD_COUNT_PER_USER - 1);
        return JsonResult.buildSuccess(new HistorySearchWordDTO(result));
    }

    /**
     * 搜索词补全接口
     *
     * @param request
     */
    @Override
    public JsonResult<CompletionSearchWordsDTO> listCompletionSearchWords(CompletionSearchWordQuery request) {
        try {
            CompletionSearchWordsDTO result = productSearchRepository.listCompletionSearchWords(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 商品搜索接口
     *
     * @param request
     */
    @Override
    public JsonResult<PorductSearchDTO> searchProducts(ProductSearchQuery request) {
        try {
            PorductSearchDTO result = productSearchRepository.searchProducts(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 商品结构化查询接口
     *
     * @param request
     */
    @Override
    public JsonResult<PorductSearchDTO> structuredSearchProducts(ProductStructuredQuery request) {
        try {
            PorductSearchDTO result = productSearchRepository.structuredSearchProducts(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.buildError(e.getMessage());
        }
    }
}
